﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 操作系统
{
    public partial class Form1 : Form
    {
        private int speed = 5;//默认速度
        private int carX = 0;//小车起始X坐标
        private int carY = 0;//小车起始Y坐标
        private Timer timer;
        public Form1()
        {
            InitializeComponent();
            timer = new Timer(); //初始化计时器
            timer.Interval = 50;//每50ms移动一次
            timer.Tick += timer1_Tick;
            //配置notifyLcon
            notifyIcon1.Visible = false;
        
            //设置窗体关闭事件
            this.FormClosing += form1_FormClosing;
            //创建菜单
            contextMenuStrip1 = new ContextMenuStrip();
            restoreToolStripMenuItem = new ToolStripMenuItem("恢复");
            restoreToolStripMenuItem.Click += RestoreToolStripMenuItem_Click;
            exitToolStripMenuItem = new ToolStripMenuItem("退出");
            exitToolStripMenuItem.Click += ExitToolStripMenuItem_Click;
            contextMenuStrip1.Items.AddRange(new ToolStripItem[] 
            {
                 restoreToolStripMenuItem,
                exitToolStripMenuItem
            });
            notifyIcon1.ContextMenuStrip = contextMenuStrip1;//关联菜单
         }
        private void Form1_Load(object sender,EventArgs e)
        {
            //开始移动小车
            timer.Start();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            // 小车主体
            Rectangle carBody = new Rectangle(carX, carY, 50, 30);
            e.Graphics.FillRectangle(Brushes.Red, carBody); // 红色车身

            // 车窗
            Rectangle window = new Rectangle(carX + 10, carY + 5, 20, 15);
            e.Graphics.FillRectangle(Brushes.LightGray, window); // 灰色车窗

            // 车轮
            Rectangle wheel1 = new Rectangle(carX + 5, carY + 25, 10, 10);
            Rectangle wheel2 = new Rectangle(carX + 35, carY + 25, 10, 10);
            e.Graphics.FillEllipse(Brushes.Black, wheel1); // 黑色车轮
            e.Graphics.FillEllipse(Brushes.Black, wheel2);
        }

        private void form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //取消关闭窗体，隐藏状态，任务栏显示图标
            e.Cancel = true;
            this.Hide();
            notifyIcon1.Visible = true;
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            //更新小车坐标
            carX += 2*speed;
            carY += speed;
            //如果小车超出窗口范围，重置其位置
            if (carX > this.ClientSize.Width || carY > this.ClientSize.Height)
            {
                carX = 0;
                carY = 0;
            }

            //重绘窗体
            this.Invalidate();
        }

        private void Button_start_Click_Click(object sender, EventArgs e)
        {
            //开始移动小车
            timer.Start();
        }

        private void Button_stop_Click(object sender, EventArgs e)
        {
            //停止移动小车
            timer.Stop();
        }

        private void Button_quick_Click(object sender, EventArgs e)
        {
            speed += 2;//加速
        }

        private void Button_slow_Click(object sender, EventArgs e)
        {
            speed = Math.Max(1, speed - 2);//减速，最低速度为1
        }

        private void RestoreToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // 恢复窗体
            this.Show();
            this.WindowState = FormWindowState.Normal;
        }

        private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // 退出程序
            Application.Exit();
            this.Dispose();
        }
    }
}
